package com.shenghua.shiro;

import com.shenghua.dao.bean.RoleBean;
import com.shenghua.dao.bean.UserBean;
import com.shenghua.dao.model.RoleLimitX;
import com.shenghua.dao.model.User;
import com.shenghua.service.LoginService;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.credential.CredentialsMatcher;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

import javax.annotation.Resource;

/**
 * @author: yzr
 * @date: 2020/2/22
 */
public class CustomRealm extends AuthorizingRealm {

    @Resource
    private LoginService loginService;

    //告诉shiro如何根据获取到的用户信息中的密码和盐值来校验密码
    //如果未设置密码校验方式，则会报如下错误
    //Submitted credentials for token did not match the expected credentials.
    @Override
    public void setCredentialsMatcher(CredentialsMatcher credentialsMatcher) {
        //设置用于匹配密码的CredentialsMatcher
        HashedCredentialsMatcher hashMatcher = new HashedCredentialsMatcher();
        hashMatcher.setHashAlgorithmName(Md5Hash.ALGORITHM_NAME);
        //hashMatcher.setStoredCredentialsHexEncoded(false);
        hashMatcher.setHashIterations(1024);
        super.setCredentialsMatcher(hashMatcher);
    }

    /**
     * 权限认证
     * @param principalCollection
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        //获取登录用户名
        String username = (String) principalCollection.getPrimaryPrincipal();
        UserBean userBean = loginService.getUserByName(username);
        //添加角色和权限
        SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
        for (RoleBean roleBean : userBean.getRoleBeanSet()) {
            //添加角色
            simpleAuthorizationInfo.addRole(roleBean.getRoles().getSign());
            //添加权限
            for (RoleLimitX roleLimitX : roleBean.getRoleLimitXList()) {
                simpleAuthorizationInfo.addStringPermission(roleLimitX.getAddr());
            }
        }
        return simpleAuthorizationInfo;
    }

    /**
     * 登录认证
     * @param token
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        //加这一步的目的是在Post请求的时候会先进认证，然后在到请求
        if (token.getPrincipal() == null) {
            return null;
        }
        //获取用户信息
        String username = token.getPrincipal().toString();
        UserBean userBean = loginService.getUserByName(username);
        if (userBean == null) {
            //这里返回后会报出对应异常
            return null;
        } else {
            User user = userBean.getUser();
            //这里验证authenticationToken和simpleAuthenticationInfo的信息
            SimpleAuthenticationInfo info = loginService.getAuthenticationInfo(userBean, user, super.getName());
            return info;
        }
    }
}
